
import * as THREE from 'three'
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'

class house3D {
  scene: THREE.Scene | null;
  moduleName: string;

  constructor(scene: THREE.Scene | null, moduleName: string){
    this.scene = scene
    this.moduleName = moduleName
    this.initMesh()
  }

  addGLTFModel(modelName:string){
    return new Promise((resolve, reject)=>{
      try{
        const loader = new GLTFLoader().setPath('3dModules/house/')
        loader.load(modelName,(gltf)=>{
          const model= gltf.scene
          model.scale.set(0.1,0.1,0.1) // 缩放
          model.position.set(0,0.1,-10) // 位置
          model.rotation.set(0.05,0,0) // 旋转
          this.scene!.add(model)
          resolve(modelName+'加载成功')
        })
      }catch(e){
        reject(modelName+'加载失败'+e)
      }
    })
  }

  initMesh(){
    this.addGLTFModel(this.moduleName).then(res=>{
      console.log(res);
    }).catch(err=>{
      console.log(err);
    })
  }
}

export default house3D